package com.example.gulimall.seckill.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.common.utils.R;
import com.example.gulimall.seckill.service.SecKillService;
import com.example.gulimall.seckill.to.SecKillRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SecKillController {

    @Autowired
    SecKillService secKillService;

    // 开启流控的方法 默认所有都会开启
    // @SentinelResource
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SecKillRedisTo> vos = secKillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping("/Sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){

        SecKillRedisTo to = secKillService.getSkukillInfo(skuId);
        return R.ok().setData(to);
    }


    @GetMapping("/kill")
    public String getKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) throws InterruptedException {

        // 判断是否登录 拦截器已经做了

        String orderSn = secKillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
